﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;

namespace SCM.Manage.Controllers
{
    [Authorize]
    public class SysRoleController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        #region 添加
        public ActionResult AddView()
        {
            return View();
        }

        public IActionResult Add(T_Role entity)
        {
            #region 数据验证
            if (string.IsNullOrWhiteSpace(entity.RoleName))
            {
                return Error("角色英文缩写不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.RoleDescription))
            {
                return Error("角色中文名称不能为空");
            }
            #endregion
            entity.ID = Guid.NewGuid().ToString();
            entity.CompanyID = CurrentCompanyID;
            entity.Creator = CurrentUserId;
            entity.CreatorName = CurrentUserName;

            var service = GetService<IRoleService>();
            bool result = service.Add(entity) != null ? true : false;
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 查询
        [HttpPost]
        public IActionResult Search(string roleName, int index = 1)
        {
            var service = GetService<IRoleService>();
            var userPage = service.Query(roleName, CurrentCompanyID, new Common.PageInfo() { PageIndex = index, PageSize = 10 });
            return GetOk(new { totalCount = userPage.Page.DataCount, items = userPage.Data });
        }

        #endregion

        #region 编辑
        public IActionResult EditView(string id)
        {
            var model = GetService<IRoleService>().GetById(id);
            return View(model);
        }

        public IActionResult Modify(T_Role entity)
        {
            entity.ModifyUserId = CurrentUserId;
            entity.ModifyUserName = CurrentUserName;

            if (entity.RoleName.Equals("Admin") || entity.RoleName.Equals("SuperAdmin"))
            {
                return Error("系统角色不能修改");
            }
            bool result = GetService<IRoleService>().Update(entity);
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 权限
        public IActionResult EditFunctionView(string id)
        {
            ViewBag.roleID = id;
            return View();
        }

        /// <summary>
        /// 根据角色获取权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public IActionResult GetFunctionByRoleID(string roleId)
        {
            var roleFunctions = GetService<IRoleFunctionService>().GetListByRoleId(CurrentCompanyID, roleId);
            return GetOk(roleFunctions);
        }

        /// <summary>
        /// 保存角色对应的权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="functionIds"></param>
        /// <returns></returns>
        public IActionResult SaveRoleFunction(string roleId, List<string> functionIds)
        {
            var result = GetService<IRoleFunctionService>().Update(roleId, CurrentCompanyID, functionIds);
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除  【支持批量操作】
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Delete(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids) || ids.Split(',').Length < 0)
            {
                return Error("请选择数据");
            }
            var delIds = ids.Split(',').ToList();

            var service = GetService<IRoleService>();
            var roles = service.GetList(delIds);
            if (roles != null && roles.Count > 0)
            {
                if (roles.Exists(p => p.RoleName.Equals("Admin") || p.RoleName.Equals("SuperAdmin")))
                {
                    return Error("系统角色不能删除");
                }
            }

            bool result = GetService<IRoleService>().Delete(delIds);
            return result ? GetOk(result) : Error("删除失败");
        }
        #endregion
    }
}
